package com.central.apps.controller;

import com.alibaba.fastjson.JSONObject;
import com.central.app.service.HandlerService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/getsuppacc")
public class GetsuppaccController {
    @Autowired
    private HandlerService handlerService;
//供应商评价
    @PostMapping("/getsuppacc")
    public String PageResult (@RequestBody List<Map<String,Object>> params){
        JSONObject json = new JSONObject();
        if (CollectionUtils.isNotEmpty(params)){
            float num =0;
            String evaluation="";
            for (int i=0;i<params.size();i++){
                Map<String, Object> map1 = params.get(i);
                String id = MapUtils.getString(map1, "id");
                Float score = MapUtils.getFloat(map1, "score");
                handlerService.updateBySql("update gq_dynamiation set score="+score+" where id='"+id+"'");
                evaluation = MapUtils.getString(map1, "evaluation");
                num = num + score;
            }
            if (num <=100 && num >=0){
                json.put("code","0");
                handlerService.updateBySql("UPDATE gq_vendorlist set finalscore ="+num+" where evaluation ='"+evaluation+"'");
                if (num>=90){
                    String grad = "优选";
                    handlerService.updateBySql("UPDATE gq_vendorlist set gradeafter ='"+grad+"' where evaluation ='"+evaluation+"'");

                }else if(num>=70&&num<90){
                    String grad = "可选";
                    handlerService.updateBySql("UPDATE gq_vendorlist set gradeafter ='"+grad+"' where evaluation ='"+evaluation+"'");

                }else  if (num>=60&&num<70){
                    String grad = "慎选";
                    handlerService.updateBySql("UPDATE gq_vendorlist set gradeafter ='"+grad+"' where evaluation ='"+evaluation+"'");

                }else {
                    String grad = "不合格";
                    handlerService.updateBySql("UPDATE gq_vendorlist set gradeafter ='"+grad+"' where evaluation ='"+evaluation+"'");

                }
                json.put("msg", "计算得分成功!");
                return json.toString() ;
            }else {
                json.put("code","1");
                json.put("msg", "评定分数不能小于0并且不能超过100!");
                return json.toString() ;
            }
        }
        return null;
    }
    //供应商准入
    @PostMapping("/appgetsuppacc")
    public String AppPageResult (@RequestBody List<Map<String,Object>> params){
        JSONObject object = new JSONObject();
        if (CollectionUtils.isNotEmpty(params)){
            Map<String, Object> map1 = params.get(0);
            String application = MapUtils.getString(map1, "application");
            //解决未选择供应商情况下，不让计算得分，应该提示请先创建供应商或者计算无效
            List<Map<String, Object>> bySql2 = handlerService.findBySql("select count(0) as count from gq_account where application = '" + application + "'");
            if(CollectionUtils.isNotEmpty(bySql2)){
                if(MapUtils.getInteger(bySql2.get(0),"count")==0){
                    object.put("code","1");
                    object.put("msg", "请先创建供应商或者计算无效!");
                    return object.toString() ;
                }
            }
            float num =0;
            for (int i=0;i<params.size();i++){
                Map<String, Object> map = params.get(i);
                String id = MapUtils.getString(map, "id");
                Float score = MapUtils.getFloat(map, "score");
                handlerService.updateBySql("update gq_evalsuppliers set score="+score+" where id='"+id+"'");
                num = num + score;
            }
            if ( num <=100 && num >=0){
                object.put("code","0");
                handlerService.updateBySql("\tUPDATE gq_account set assessment ="+num+" where application ='"+application+"'");
                object.put("msg", "计算得分成功!");
                return object.toString() ;
            }else{
                object.put("code","1");
                object.put("msg", "评定分数不能小于0并且不能超过100!");
                return object.toString() ;

            }
        }
        return null;
    }
}
